package zhoufu.sz.sanxiao_check.ui;

import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import javax.inject.Inject;
import zhoufu.sz.sanxiao_check.App;
import zhoufu.sz.sanxiao_check.R;
import zhoufu.sz.sanxiao_check.inject.components.AppComponent;
import zhoufu.sz.sanxiao_check.inject.modules.ActivityModule;
import zhoufu.sz.sanxiao_check.navigation.Navigator;
import zhoufu.sz.sanxiao_check.ui.fragment.FireSafetyFragment;

/**
 * Created by hyc on 16/5/5.
 * 获取AppComponent
 * 初始化Toolbar,并暴露一些方法来控制toolbar
 */
public abstract class BaseActivity extends AppCompatActivity
        implements DialogInterface.OnCancelListener
{
    @Inject
    public Navigator _navigator;

    private App _application;

    public Toolbar toolbar;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.getAppComponent().inject(this);
        _application.addActivity(this);
        android.databinding.DataBindingComponent component = setBindingComponent();
        ViewDataBinding                          binding;
        binding = null ==
                component ? DataBindingUtil.setContentView(this, setIndividualId()) :
                  DataBindingUtil.setContentView(this, setIndividualId(),component);
        toolbar = (Toolbar) binding.getRoot().findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        initInject();
        initAfterOnCreate(binding);

    }

    @Nullable
    protected android.databinding.DataBindingComponent setBindingComponent() {
        return null;
    }

    protected abstract void initInject();

    protected void initAfterOnCreate(ViewDataBinding binding) {

    }

    /**
     * 返回各自要初始化的 页面 xml id
     */
    protected abstract int setIndividualId();

    /**
     * 返回初始化后的DataBindComponent
     * @return
     */


    /**
     * Adds a {@link Fragment} to this activity's layout.
     *
     * @param containerViewId The container view to where add the fragment.
     * @param fragment        The fragment to be added.
     */
    public void addFragment(int containerViewId, Fragment fragment) {
        if (fragment==null)return;
        FragmentTransaction transaction = this.getSupportFragmentManager().beginTransaction();

        transaction.add(containerViewId, fragment,fragment.getClass().getCanonicalName());

        transaction.commitAllowingStateLoss();

    }
    public void addFragmentWithStage(int id, Fragment fragment){
        FragmentTransaction transaction =this.getSupportFragmentManager().beginTransaction();
        transaction.replace(id,fragment).addToBackStack(null);
        transaction.commitAllowingStateLoss();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            onBackPressed();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    protected AppComponent getAppComponent() {
        _application = (App) getApplication();
        return _application.getAppComponent();
    }

    public Navigator getNavigator() {
        return _navigator;
    }

    protected ActivityModule getActivityModule() {
        return new ActivityModule(this);
    }

    public void popFragment(final Fragment fragment){
        fragment.getView().postDelayed(new Runnable() {
            @Override
            public void run() {
                getSupportFragmentManager().
                        popBackStack(fragment.getClass().getSimpleName(), FragmentManager.POP_BACK_STACK_INCLUSIVE);
            }
        }, 300);
    }

    ProgressDialog _progressDialog;

    protected void showProgreesDialog() {
        if (_progressDialog == null) {
            _progressDialog = ProgressDialog.show(this, getString(R.string.insprition), getString(
                    R.string.dialog_progress_message)
                    , true, false, this);
        }
        _progressDialog.show();
    }

    protected void hideProgreesDialog() {
        if (_progressDialog != null)
            _progressDialog.dismiss();
    }

    @Override public void onCancel(DialogInterface dialog)
    {

    }
}
